<?php
/**
 * Created by PhpStorm.
 * User: zhangrxiang
 * Date: 2017/7/13
 * Time: 17:04
 * Project name: PHPTEST
 * File name: 7.php
 * @copyright Copyright (c) 2016 ZRX Software LLC
 * @license https://github.com/zhangrxiang
 * @since 1.0.0
 */
 function actionSetpwd( $type = 'email' ) {
	$this->layout = 'member';
	if ( RegisterService::EMAIL === $type ) {
		$email = Yii::app()->session['email'];
		//重置密码
		if ( ! Yii::app()->user->isGuest ) {
			$num = Yii::app()->user->name;
			if ( isset( $_POST['member'] ) ) {
				$numinfo = Member::getMemberInfoByEmail( $email );
				$pwd     = md5( $_POST['member']['password'] );
				$count   = Member::model()->updateByPk( $numinfo->id, array( 'password' => $pwd ) );
				if ( $count > 0 ) {
					$this->redirect( 'resetnewpwdok' );
				} else {
					echo $this->redirect( 'register' );
				}
			}
		}
		if ( ! $email ) {
			$this->redirect( 'register' );
		}

		if ( isset( $_POST['member'] ) ) {
			if ( Yii::app()->session['statuslogin'] ) {
				$pwd     = md5( $_POST['member']['password'] );
				$numinfo = Member::getMemberInfoByEmail( $email );
				$count   = Member::model()->updateByPk( $numinfo->id, array( 'password' => $pwd ) );
				if ( $count > 0 ) {
					$this->redirect( 'resetnewpwdok' );
				} else {
					$this->redirect( 'resetnewpwdok' );
				}
			}
			Yii::app()->session['password'] = md5( $_POST['member']['password'] );
			$this->redirect( array( 'information' ) );
		}
		$this->render( 'setpwd' );

	} else {
		$phone = Yii::app()->session['tel'];
		//重置密码
		if ( ! Yii::app()->user->isGuest ) {
			$num = Yii::app()->user->name;
			// echo $num;exit;
			if ( isset( $_POST['member'] ) ) {
				$numinfo = Member::model()->find( 'tel=:tel', array( ':tel' => $num ) );
				$pwd     = md5( $_POST['member']['password'] );
				$count   = Member::model()->updateByPk( $numinfo->id, array( 'password' => $pwd ) );
				if ( $count > 0 ) {
					$this->redirect( 'resetnewpwdok' );
				} else {
					echo $this->redirect( 'register' );
				}
			}
		}
		if ( ! $phone ) {
			$this->redirect( 'register' );

		}

		if ( isset( $_POST['member'] ) ) {
			if ( strlen( $phone ) < 11 ) {
				exit();
			}
			if ( Yii::app()->session['statuslogin'] ) {
				$numinfo = Member::model()->find( 'tel=:tel', array( ':tel' => $phone ) );
				$pwd     = md5( $_POST['member']['password'] );
				$count   = Member::model()->updateByPk( $numinfo->id, array( 'password' => $pwd ) );
				if ( $count > 0 ) {
					$this->redirect( 'resetnewpwdok' );
				} else {
					$this->redirect( 'resetnewpwdok' );
				}
			}
			Yii::app()->session['password'] = md5( $_POST['member']['password'] );
			$this->redirect( array( 'information' ) );
		}
		$this->render( 'setpwd' );
	}

}
